//
//  XWNavViewController.m
//  CGWSWW
//
//  Created by lanou3g on 16/8/12.
//  Copyright © 2016年 CXG. All rights reserved.
//

#import "XWNavViewController.h"
#import "UIImage+XWExtension.h"
@interface XWNavViewController ()<UIGestureRecognizerDelegate>

@end

@implementation XWNavViewController
+(void)initialize{
    //设置UIBarButtonItem状态下的字体样式
    [ [UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[self]]setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]} forState:(UIControlStateNormal)];
    [ [UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[self]]setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor orangeColor]} forState:(UIControlStateSelected)];
    //设置UINavigationBartitle字体样式
    [ [UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[self]]setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:19]}];
    //设置navigationBar背景图片
    [[UINavigationBar appearance]  setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithRed:206 green:206 blue:206 alpha:1]] forBarMetrics:(UIBarMetricsDefault)];
}


#pragma mark -自定义push控制器UINavigationBar上的返回按钮
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if (self.childViewControllers.count) {
        UIButton *btn =[UIButton buttonWithType:(UIButtonTypeCustom)];
        [btn setImage:[UIImage imageNamed:@"back_deselected"] forState:(UIControlStateNormal)];
        [btn setImage:[UIImage imageNamed:@"back_selected"] forState:(UIControlStateHighlighted)];
        [btn setTitle:@"返回" forState:(UIControlStateNormal)];
        [btn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [btn setTitleColor:[UIColor orangeColor] forState:(UIControlStateHighlighted)];
        //注意让btn的frame的size调整为与内容image的size一致
        [btn sizeToFit];
        [btn addTarget:self action:@selector(backClick:) forControlEvents:(UIControlEventTouchUpInside)];
        btn.contentEdgeInsets =UIEdgeInsetsMake(0, -20, 0, 0);//设置偏移量调整btn在UINavigationBar的位置
        viewController.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc]initWithCustomView:btn];
        //隐藏底部状态栏
        viewController.hidesBottomBarWhenPushed =YES;
    }
    [super pushViewController:viewController animated:animated];
}

//监听返回按钮上的点击事件
-(void)backClick:(UIBarButtonItem*)buttonItem{
    [self popViewControllerAnimated:YES];
}
-(void)viewDidLoad {
    [super viewDidLoad];
    //设置手势代理(规避在push过程中造成的返回手势失效)
    self.interactivePopGestureRecognizer.delegate =self;
}
//手势识别器
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    if (self.childViewControllers.count>1) {
        return YES;
    }
    else
        return NO;
}

@end
